﻿// 功能：红外探测API封装
// 描述：
// 作者：温伟鹏
// 日期：2010/6/2 9:44:12

using System;
using System.Runtime.InteropServices;

namespace MeizuSDK.Core
{
    /// <summary>
    /// 红外探测传感器API
    /// 必须先注册传感器消息，然后才能调用API函数
    /// </summary>
    public static class IRSensor
    {
        #region DllImport
        /// <summary>
        /// 获取红外探测的探测状态
        /// </summary>
        /// <param name="pbDetection">存储探测状态的数据指针,FALSE - 没有探测到物体 TRUE - 探测到物体</param>
        /// <returns>FALSE - 失败, TRUE - 成功</returns>
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool IRSensorGetIRSensorDetection(ref bool pbDetection);
        /// <summary>
        /// 注册红外探测的消息通知
        /// @lParam :[out]收到的消息通知参数,0:没有探测到物体  1:探测到物体
        /// </summary>
        /// <param name="hWnd">红外探测消息通知的窗口句柄</param>
        /// <param name="msgId">红外探测消息通知的消息ID</param>
        /// <returns>FALSE - 失败, TRUE - 成功</returns>
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool IRSensorRegisterIRSensorNotify(IntPtr hWnd, uint msgId);
        /// <summary>
        /// 注销红外探测的消息通知
        /// </summary>
        /// <param name="hWnd">要注销的红外探测消息通知的窗口句柄</param>
        /// <returns>FALSE - 失败, TRUE - 成功</returns>
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool IRSensorUnRegisterIRSensorNotify(IntPtr hWnd);
        /// <summary>
        /// 设置红外探测的开关状态
        /// </summary>
        /// <param name="bOpened"> FALSE - 关闭红外探测      TRUE - 打开红外探测</param>
        /// <returns>FALSE - 失败, TRUE - 成功</returns>
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool IRSensorSetIRSensorState(bool bOpened);
        /// <summary>
        /// 设置红外探测等级
        /// </summary>
        /// <param name="level">[in]要设置的红外探测等级 LEVEL_HIGH LEVEL_MEDIUM LEVEL_LOW</param>
        /// <returns>FALSE - 失败, TRUE - 成功</returns>
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool IRSensorSetIRSensorLevel(uint level);
        /// <summary>
        /// 获取红外探测等级
        /// </summary>
        /// <returns>0 - 失败, >0 - 红外探测等级 LEVEL_HIGH LEVEL_MEDIUM LEVEL_LOW</returns>
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern uint IRSensorGetIRSensorLevel();
        #endregion

        #region 函数
        /// <summary>
        /// 注册红外探测的消息通知
        /// @lParam :[out]收到的消息通知参数,0:没有探测到物体  1:探测到物体
        /// </summary>
        /// <param name="hWnd">红外探测消息通知的窗口句柄</param>
        /// <param name="irSensorMsgId">红外探测消息通知的消息ID</param>
        /// <returns>返回true，表示注册成功；否则表示注册失败</returns>
        public static bool RegisterNotify(IntPtr hWnd, uint irSensorMsgId)
        {
            return IRSensorRegisterIRSensorNotify(hWnd, irSensorMsgId);
        }
        /// <summary>
        /// 注销红外探测的消息通知
        /// </summary>
        /// <param name="hWnd">要注销的红外探测消息通知的窗口句柄</param>
        /// <returns>返回true，表示注销成功；否则表示注销失败</returns>
        public static bool UnRegisterNotify(IntPtr hWnd)
        {
            return IRSensorUnRegisterIRSensorNotify(hWnd);
        }
        /// <summary>
        /// 打开红外探测传感器
        /// </summary>
        /// <returns>返回true，表示打开成功，否则表示打开失败</returns>
        public static bool Open()
        {
            return IRSensorSetIRSensorState(true);
        }
        /// <summary>
        /// 关闭红外探测传感器
        /// </summary>
        /// <returns>返回true，表示关闭成功；否则表示关闭失败</returns>
        public static bool Close()
        {
            return IRSensorSetIRSensorState(false);
        }
        /// <summary>
        /// 获取红外探测的探测状态
        /// </summary>
        /// <returns>false - 没有探测到物体 true - 探测到物体</returns>
        public static bool GetDetection()
        {
            bool detection = false;
            bool result = IRSensorGetIRSensorDetection(ref detection);

            if (result)
                return detection;
            return false;
        }
        /// <summary>
        /// 获取红外探测等级
        /// </summary>
        /// <returns>返回的红外探测等级</returns>
        public static IRSensorLevel GetLevel()
        {
            uint result = IRSensorGetIRSensorLevel();

            if(Enum.IsDefined(typeof(IRSensorLevel),result))
            {
                return (IRSensorLevel)result;
            }
            else
            {
                return IRSensorLevel.None;
            }
        }
        /// <summary>
        /// 设置红外探测等级
        /// </summary>
        /// <param name="level">要设置的红外探测等级</param>
        /// <returns>返回true，表示设置成功；否则表示设置失败</returns>
        public static bool SetLevel(IRSensorLevel level)
        {
            return IRSensorSetIRSensorLevel((uint)level);
        }
        #endregion
    }
    /// <summary>
    /// 红外探测等级
    /// </summary>
    public enum IRSensorLevel : uint
    {
        None = 0,
        High = 1,
        Medium = 2,
        Low = 3,
        Max = 4
    }
}
